<?php
class RecordsHelper
{
	public static $TYPE_TODAY = 'today';
	public static $TYPE_THREE_DAY = 'threeday';
	public static $TYPE_WEEK = 'week';
	public static $TYPE_MONTH = 'month';
	public function getRecordsByType($type)
	{
		$date_list  = $this->getDatesBytype($type);
		$data_list = array();
		foreach($date_list as $tmpData)
		{
			$strTokenList = preg_split("/-/",$tmpData);
			$logDir = constant('LOG_DATA_DIR').'/'.$strTokenList[0].'/'.$strTokenList[1].'/'.$strTokenList[2];
			if(file_exists($logDir))
			{
				$dayRecords = array();
				$dp = opendir($logDir);
 				while (($file=readdir($dp)) != false) 
 				{
					$filePath = $logDir.'/'.$file;
					if(!is_file($filePath))
						continue;
					 $record = $this->parseRecord(file_get_contents($filePath));
					  $record['date'] = $tmpData;
					  $rKey = substr($file,0,strpos($file,'_'));
					 $dayRecords[ $rKey] =  $record;
				}
				 closedir($dp);
				 ksort($dayRecords,SORT_NUMERIC);
				 foreach($dayRecords as $record)
				 {
				 	 $data_list[]  = $record;
				 }
			}
		}
		return $data_list;
	}
	public function getDatesBytype($type)
	{
		$date_list = array();
		$currentTime = AppMain::getCurrentTime();
		
		$t_currentTime = strtotime($currentTime);
		switch($type)
		{
			case RecordsHelper::$TYPE_TODAY:
				$date_list[] = $currentTime;
			break;
			case RecordsHelper::$TYPE_THREE_DAY:
				for($i = 2; $i > -1; $i--)
				{
					if($i == 0)
					{
						$date_list[] = $currentTime ;
					}else{
						$date_list[] = date('Y-m-d',strtotime("-".$i." day",$t_currentTime));
					}
				}
			break;
			case RecordsHelper::$TYPE_WEEK:
				$dayOfWeek = date('w',$t_currentTime);
				if($dayOfWeek == 0)
					$dayOfWeek = 7;
				for($i = ($dayOfWeek - 1); $i > -1; $i--)
				{
					if($i == 0)
					{
						$date_list[] = $currentTime ;
					}else{
						$date_list[] = date('Y-m-d',strtotime("-".$i." day",$t_currentTime));
					}
				}
			break;
			case RecordsHelper::$TYPE_MONTH:
				$fYear = isset($_REQUEST['fYear'])?$_REQUEST['fYear']:date('Y',$t_currentTime);
				$fMonth = isset($_REQUEST['fMonth'])?$_REQUEST['fMonth']:date('m',$t_currentTime);
		
				$selectDatetime = strtotime(sprintf("%s-%s-1",$fYear,$fMonth));
				$currentDatetime = strtotime(sprintf("%s-%s-1",date('Y',$t_currentTime),date('n',$t_currentTime)));
				
				if($selectDatetime == $currentDatetime)
				{
				$dateOfMonth = date('j',$t_currentTime);
				for($i = ($dateOfMonth - 1); $i > -1; $i--)
				{
					if($i == 0)
					{
						$date_list[] = $currentTime ;
					}else{
						$date_list[] = date('Y-m-d',strtotime("-".$i." day",$t_currentTime));
					}
				}
				}else{
					$monthDays = cal_days_in_month(CAL_GREGORIAN, $fMonth, $fYear);
					$monthDays++;
					for($i=1; $i < $monthDays; $i++)
					{
						$date_list[] = sprintf("%s-%s-%s",$fYear,$fMonth,$i);
					}
				}
			break;
		}
		return $date_list;
	}
	public function parseRecord($strContent)
	{
		$strContent = substr($strContent,strlen(constant('SEPERATER_1'))+4);
		$data = false;
		if($strContent[0] == 'D')
		{
			$data = $this->parseNormalAddRecord($strContent);
		}else if($strContent[0] == 'T'){
			$data = $this->parseTransRecord($strContent);
		}
		return $data;
	}
	private function parseNormalAddRecord($strContent)
	{
		$data = array();
		$data['type'] = constant('RECORD_TYPE_NORMAL_PAY');
		
		$strContent = substr($strContent,strpos($strContent,'Total') + 6);
		$data['total'] = substr($strContent,0,strpos($strContent,'<br>'));
		
		$strContent = substr($strContent,strpos($strContent,'Detail') + 7);
		$data['detail'] = substr($strContent,0,strpos($strContent,constant('SEPERATER_2')));
		
		$strContent = substr($strContent,strpos($strContent,'Assign Info') + 16);
		$assignInfo = substr($strContent,0,strpos($strContent,constant('SEPERATER_2')));
		$tokenList = preg_split('/<br>/',$assignInfo);
		foreach($tokenList as $tmpData)
		{
			if(strlen($tmpData) == 0)
				continue;
			$tokenList = preg_split('/:/', $tmpData);
			$data['assign'][$tokenList[0]] = $tokenList[1];
		}
		
		$strContent = substr($strContent,strpos($strContent,'User:'));
		$createInfo = substr($strContent,0,strpos($strContent,constant('SEPERATER_3')));
		$tokenList = preg_split('/<br>/',$createInfo);
		foreach($tokenList as $tmpData)
		{
			if(strlen($tmpData) == 0)
				continue;
			$tokenList = preg_split('/:/', $tmpData);
			$data['createInfo'][$tokenList[0]] = $tokenList[1];
		}
		
		$strContent = substr($strContent,strpos($strContent,'Account Balance:') + 20);
		$balanceInfo = substr($strContent,0,strpos($strContent,constant('SEPERATER_1')));
		$tokenList = preg_split('/<br>/',$balanceInfo);
		foreach($tokenList as $tmpData)
		{
			if(strlen($tmpData) == 0)
				continue;
			$tokenList = preg_split('/:/', $tmpData);
			$data['balanceInfo'][$tokenList[0]] = $tokenList[1];
		}
		
		return $data;
	}
	private function parseTransRecord($strContent)
	{
		$data = array();
		$data['type'] = constant('RECORD_TYPE_TRANS');
		$strContent = substr($strContent,strpos($strContent,'Transe') + 6);
		
		$data['total'] = substr($strContent,0,strpos($strContent,'from') - 1);

		
		$strContent = substr($strContent,strpos($strContent,'[') + 1);
		$data['from'] = substr($strContent,0,strpos($strContent,']'));
		
		$strContent = substr($strContent,strpos($strContent,'[') + 1);
		$data['to'] = substr($strContent,0,strpos($strContent,']'));
	
		$strContent = substr($strContent,strpos($strContent,'Account Balance:') + 20);
		$balanceInfo = substr($strContent,0,strpos($strContent,constant('SEPERATER_1')));
		$tokenList = preg_split('/<br>/',$balanceInfo);
		foreach($tokenList as $tmpData)
		{
			if(strlen($tmpData) == 0)
				continue;
			$tokenList = preg_split('/:/', $tmpData);
			$data['balanceInfo'][$tokenList[0]] = $tokenList[1];
		}
		return $data;
	}
}